<?php
namespace app\index\controller;

use app\index\model\AccountDiaoModel;
use app\index\model\AccountListModel;
use app\index\model\AccountModel;
use app\index\model\DealcomGengModel;
use app\index\model\DealcomModel;
use app\index\model\DeptModel;
use app\index\model\EnginBudshouModel;
use app\index\model\EnginBudsubModel;
use app\index\model\EnginChuModel;
use app\index\model\EnginModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\IncomeEnginModel;
use app\index\model\IncomeQiModel;
use app\index\model\PaymentBaoModel;
use app\index\model\PaymentDayListModel;
use app\index\model\PaymentEnginModel;
use app\index\model\PaymentGongListModel;
use app\index\service\BudgetService;
use app\index\service\FinanceService;
use app\index\service\FlowService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\PaymentService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Finance extends Common
{
    public function list_dealcom(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['deal.deleted_user','=', 0];
        $where[] = ['deal.comid','=',$this->userinfo['comid']];
        $where[] = ['deal.isturn','=',1];
        $where[] = ['deal.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['deal.name|deal.ratenum|deal.phone|deal.banknum|deal.lianer|deal.liantel','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['deal.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['type']) && !empty($param['type'])){
            $where[] = ['deal.type','=', $param['type']];
        }
        if(isset($param['nature']) && !empty($param['nature'])){
            $where[] = ['deal.nature','=', $param['nature']];
        }
        if(isset($param['legaler']) && !empty($param['legaler'])){
            $where[] = ['deal.legaler','like', '%'.$param['legaler'].'%'];
        }
        if(isset($param['zhuaddr']) && !empty($param['zhuaddr'])){
            $where[] = ['deal.zhuaddr','like', '%'.$param['zhuaddr'].'%'];
        }
        if(isset($param['taxtype']) && !empty($param['taxtype'])){
            $where[] = ['deal.taxtype','=', $param['taxtype']];
        }
        if(isset($param['ratenum']) && !empty($param['ratenum'])){
            $where[] = ['deal.ratenum','like', '%'.$param['ratenum'].'%'];
        }
        if(isset($param['unitaddr']) && !empty($param['unitaddr'])){
            $where[] = ['deal.unitaddr','like', '%'.$param['unitaddr'].'%'];
        }
        if(isset($param['phone']) && !empty($param['phone'])){
            $where[] = ['deal.phone','like', '%'.$param['phone'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['deal.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['deal.banknum','like', '%'.$param['banknum'].'%'];
        }
        if(isset($param['genre']) && !empty($param['genre'])){
            $where[] = ['deal.genre','like', '%'.$param['genre'].'%'];
        }
        if(isset($param['danlevel']) && !empty($param['danlevel'])){
            $where[] = ['deal.danlevel','=', $param['danlevel']];
        }
        if(isset($param['lianer']) && !empty($param['lianer'])){
            $where[] = ['deal.lianer','like', '%'.$param['lianer'].'%'];
        }
        if(isset($param['liantel']) && !empty($param['liantel'])){
            $where[] = ['deal.liantel','like', '%'.$param['liantel'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['deal.status','=', $param['status']];
        }

        $data['type'] = (new OptService())->get_base_info('cai_dealcom_type');
        $data['is_type'] = isset($param['type']) ? $param['type'] : '';

        $data['taxtype'] = (new OptService())->get_base_info('cai_dealcom_taxtype');
        $data['is_taxtype'] = isset($param['taxtype']) ? $param['taxtype'] : '';

        $data['nature'] = (new OptService())->get_base_info('cai_dealcom_nature');
        $data['is_nature'] = isset($param['nature']) ? $param['nature'] : '';

        $data['danlevel'] = (new OptService())->get_base_info('cai_dealcom_danlevel');
        $data['is_danlevel'] = isset($param['danlevel']) ? $param['danlevel'] : '';


        $data['data'] = DealcomModel::alias('deal')
            ->field('deal.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('dealcom', 'deal'))
            ->order('deal.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['type'] = (new OptService())->get_base_showname('cai_dealcom_type', $item['type']);
                $item['nature'] = (new OptService())->get_base_showname('cai_dealcom_nature', $item['nature']);
                $item['taxtype'] = (new OptService())->get_base_showname('cai_dealcom_taxtype', $item['taxtype']);
                $item['danlevel'] = (new OptService())->get_base_showname('cai_dealcom_danlevel', $item['danlevel']);
                $item['piaotype'] = (new OptService())->get_base_showname('cai_dealcom_piaotype', $item['piaotype']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('dealcom');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_dealcom(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['name'])){
                return outputJson(4000, '请输入单位名称');
            }
            if((new FinanceService())->check_dealcom_name($param['name'])){
                return outputJson(4000, '单位名称已存在');
            }
            if(empty($param['type'])){
                return outputJson(4000, '请选择所属分类');
            }
            if(empty($param['nature'])){
                return outputJson(4000, '请选择单位性质');
            }



            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = DealcomModel::insertGetId($param);
                (new OptService())->relation_file($files,'dealcom',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'dealcom', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'dealcom', $id, '往来单位');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['type'] = (new OptService())->get_base_info('cai_dealcom_type');
            $data['taxtype'] = (new OptService())->get_base_info('cai_dealcom_taxtype');
            $data['nature'] = (new OptService())->get_base_info('cai_dealcom_nature');
            $data['danlevel'] = (new OptService())->get_base_info('cai_dealcom_danlevel');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_dealcom(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if(empty($param['name'])){
                return outputJson(4000, '请输入单位名称');
            }
            if((new FinanceService())->check_dealcom_name($param['name'], $param['id'])){
                return outputJson(4000, '单位名称已存在');
            }
            if(empty($param['type'])){
                return outputJson(4000, '请选择所属分类');
            }
            if(empty($param['nature'])){
                return outputJson(4000, '请选择单位性质');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                DealcomModel::update($param);
                (new OptService())->relation_file($files,'dealcom',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'dealcom', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'dealcom', $param['id'], '往来单位');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DealcomModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['type'] = (new OptService())->get_base_info('cai_dealcom_type');
            $data['taxtype'] = (new OptService())->get_base_info('cai_dealcom_taxtype');
            $data['nature'] = (new OptService())->get_base_info('cai_dealcom_nature');
            $data['danlevel'] = (new OptService())->get_base_info('cai_dealcom_danlevel');
            $data['files'] = (new OptService())->get_file('dealcom', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_dealcom(Request $request){
        $param = $request->post();
        $data = DealcomModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            DealcomModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'dealcom', $data['id'], '往来单位');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_dealcom(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = DealcomModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['type'] = (new OptService())->get_base_showname('cai_dealcom_type', $data['data']['type']);
        $data['data']['piaotype'] = (new OptService())->get_base_showname('cai_dealcom_piaotype', $data['data']['piaotype']);
        $data['data']['nature'] = (new OptService())->get_base_showname('cai_dealcom_nature', $data['data']['nature']);
        $data['data']['taxtype'] = (new OptService())->get_base_showname('cai_dealcom_taxtype', $data['data']['taxtype']);
        $data['data']['danlevel'] = (new OptService())->get_base_showname('cai_dealcom_danlevel', $data['data']['danlevel']);
        $data['files'] = (new OptService())->get_file('dealcom', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function show_dealcom_geng(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = DealcomGengModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $dealid_info = DealcomModel::where(['id'=>$data['data']['dealid']])->find();
        $data['dealid_info'] = $dealid_info;

        $data['data']['type'] = (new OptService())->get_base_showname('cai_dealcom_type', $data['data']['type']);
        $data['data']['piaotype'] = (new OptService())->get_base_showname('cai_dealcom_piaotype', $data['data']['piaotype']);
        $data['data']['nature'] = (new OptService())->get_base_showname('cai_dealcom_nature', $data['data']['nature']);
        $data['data']['taxtype'] = (new OptService())->get_base_showname('cai_dealcom_taxtype', $data['data']['taxtype']);
        $data['data']['danlevel'] = (new OptService())->get_base_showname('cai_dealcom_danlevel', $data['data']['danlevel']);
        $data['files'] = (new OptService())->get_file('dealcom_geng', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_account(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['acc.deleted_user','=', 0];
        $where[] = ['acc.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['acc.name|acc.accname|acc.accnum','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['acc.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['accnum']) && !empty($param['accnum'])){
            $where[] = ['acc.accnum','like', '%'.$param['accnum'].'%'];
        }
        if(isset($param['acctype']) && !empty($param['acctype'])){
            $where[] = ['acc.acctype','=', $param['acctype']];
        }
        if(isset($param['accmoney']) && !empty($param['accmoney'])){
            $where[] = ['acc.accmoney','like', '%'.$param['accmoney'].'%'];
        }
        if(isset($param['accdt']) && !empty($param['accdt'])){
            $where[] = ['acc.accdt','between', explode(' - ', $param['accdt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['acc.chanid','=', $param['chanid']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['acctype'] = (new OptService())->get_base_info('cai_account_acctype');
        $data['is_acctype'] = isset($param['acctype']) ? $param['acctype'] : '';



        $whereJiao = [];
        if(isset($param['dt']) && !empty($param['dt'])){
            $whereJiao[] = ['dt','between', explode(' - ', $param['dt'])];
        }

        $data['data'] = AccountModel::alias('acc')
            ->field('acc.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('account', 'acc'))
            ->order('acc.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($whereJiao){
                $item['acctype'] = (new OptService())->get_base_showname('cai_account_acctype', $item['acctype']);
                $item['chanid_name'] = DeptModel::where(['id'=>$item['chanid']])->value('name');


                $item['shou'] = AccountListModel::where(['accid'=>$item['id']])->where($whereJiao)->where('type','in',[1,2,8,9,11,14])->sum('money');
                $item['zhi'] = AccountListModel::where(['accid'=>$item['id']])->where($whereJiao)->where('type','not in',[1,2,8,9,11,14])->sum('money');
                $item['cha'] = round($item['shou'] - $item['zhi'], 2);
                $item['balance'] = round($item['accmoney'] + $item['shou'] - $item['zhi'], 2);

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('account');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_account(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['name'])){
                return outputJson(4000, '请输入账户名称');
            }
            if(empty($param['accname'])){
                return outputJson(4000, '请输入开户行');
            }
            if(empty($param['accnum'])){
                return outputJson(4000, '请输入账号');
            }
            if(empty($param['acctype'])){
                return outputJson(4000, '请选择账户类型');
            }
            if($param['accmoney'] == ''){
                return outputJson(4000, '请输入期初金额');
            }
            if(empty($param['accdt'])){
                return outputJson(4000, '请选择期初日期');
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = AccountModel::insertGetId($param);
                (new OptService())->relation_file($files,'account',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'account', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'account', $id, '资金账户');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['acctype'] = (new OptService())->get_base_info('cai_account_acctype');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_account(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if(empty($param['name'])){
                return outputJson(4000, '请输入账户名称');
            }
            if(empty($param['accname'])){
                return outputJson(4000, '请输入开户行');
            }
            if(empty($param['accnum'])){
                return outputJson(4000, '请输入账号');
            }
            if(empty($param['acctype'])){
                return outputJson(4000, '请选择账户类型');
            }
            if($param['accmoney'] == ''){
                return outputJson(4000, '请输入期初金额');
            }
            if(empty($param['accdt'])){
                return outputJson(4000, '请选择期初日期');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                AccountModel::update($param);
                (new OptService())->relation_file($files,'account',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'account', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'account', $param['id'], '资金账户');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AccountModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['data']['chanid_info'] = (new UserService())->get_users_part_arr($data['data']['chanid'], 'dept');
            $data['acctype'] = (new OptService())->get_base_info('cai_account_acctype');
            $data['files'] = (new OptService())->get_file('account', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_account(Request $request){
        $param = $request->post();
        $data = AccountModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            AccountModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'account', $data['id'], '资金账户');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_account(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = AccountModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['data']['status_name'] = $data['data']['status'] > 0 ? '正常' : '停用';
        $data['data']['chanid_name'] = DeptModel::where(['id'=>$data['data']['chanid']])->value('name');
        $data['data']['acctype'] = (new OptService())->get_base_showname('cai_account_acctype', $data['data']['acctype']);
        $data['files'] = (new OptService())->get_file('account', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_account_shou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        $where[] = ['acc.deleted_user','=',0];
        if(isset($param['type']) && !empty($param['type'])){
            $where[] = ['list.type','=', $param['type']];
        }else{
            if(isset($param['typearr']) && is_array($param['typearr']) && count($param['typearr']) > 0){
                $where[] = ['list.type','in',$param['typearr']];
            }else{
                $where[] = ['list.type','in',[1,2,8,9,11,14]];
            }
        }

        if(isset($param['mid']) && !empty($param['mid'])){
            $where[] = ['list.mid','=', $param['mid']];
        }


        if(isset($param['bidid']) && !empty($param['bidid'])){
            $where[] = ['list.bidid','=', $param['bidid']];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['list.enginid','=', $param['enginid']];
        }
        if(isset($param['budid']) && !empty($param['budid'])){
            $where[] = ['list.budid','=', $param['budid']];
        }

        if(isset($param['jiaonum']) && !empty($param['jiaonum'])){
            $where[] = ['list.jiaonum','like', '%'.$param['jiaonum'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['list.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['list.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['list.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['daotype']) && !empty($param['daotype'])){
            $where[] = ['list.daotype','like', '%'.$param['daotype'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['list.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['month']) && !empty($param['month'])){
            $seamonth = explode(' - ', $param['month']);
            $firstday = $seamonth[0].'-01';
            $ssfist = $seamonth[1].'-01';
            $lastday = date('Y-m-d', strtotime("$ssfist +1 month -1 day"));
            $where[] = ['list.dt','between', [$firstday, $lastday]];
        }


        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.name','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }


        $data['type'] = (new FinanceService())->get_account_list_type();
        $data['is_type'] = isset($param['type']) ? $param['type'] : '';


        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['acc.chanid','=', $param['chanid']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['data'] = AccountListModel::alias('list')
            ->field('list.*,acc.name as accname,acc.accnum,e.name as enginname,bid.name as bidname')
            ->leftJoin('account acc','acc.id=list.accid')
            ->leftJoin('bid bid','bid.id=list.bidid')
            ->leftJoin('engin e','e.id=list.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('account_shou', 'list'))
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['bidid'] > 0){
                    $item['enginname'] = $item['bidname'];
                }

                $item['budname'] = (new BudgetService())->get_budid_quanname($item['budid']);
                $item['budname'] = EnginBudshouModel::where(['id'=>$item['budid']])->value('name');
                $item['accname'] = $item['accname'].'('.$item['accnum'].')';
                $item['midinfo'] = (new FinanceService())->get_account_list_midinfo($item['type'], $item['mid']);

                $item['type_name'] = (new FinanceService())->get_account_list_type_name($item['type']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_account_zhi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        $where[] = ['acc.deleted_user','=',0];
        if(isset($param['type']) && !empty($param['type'])){
            $where[] = ['list.type','=', $param['type']];
        }else{
            if(isset($param['typearr']) && is_array($param['typearr']) && count($param['typearr']) > 0){
                $where[] = ['list.type','in',$param['typearr']];
            }else{
                $where[] = ['list.type','not in',[1,2,8,9,11,14]];
            }
        }

        if(isset($param['mid']) && !empty($param['mid'])){
            $where[] = ['list.mid','=', $param['mid']];
        }


        if(isset($param['bidid']) && !empty($param['bidid'])){
            $where[] = ['list.bidid','=', $param['bidid']];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['list.enginid','=', $param['enginid']];
        }
        if(isset($param['budid']) && !empty($param['budid'])){
            $where[] = ['list.budid','=', $param['budid']];
        }

        if(isset($param['jiaonum']) && !empty($param['jiaonum'])){
            $where[] = ['list.jiaonum','like', '%'.$param['jiaonum'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['list.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['list.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['list.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['daotype']) && !empty($param['daotype'])){
            $where[] = ['list.daotype','like', '%'.$param['daotype'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['list.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['month']) && !empty($param['month'])){
            $seamonth = explode(' - ', $param['month']);
            $firstday = $seamonth[0].'-01';
            $ssfist = $seamonth[1].'-01';
            $lastday = date('Y-m-d', strtotime("$ssfist +1 month -1 day"));
            $where[] = ['list.dt','between', [$firstday, $lastday]];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.name','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }

        $data['type'] = (new FinanceService())->get_account_list_type();
        $data['is_type'] = isset($param['type']) ? $param['type'] : '';



        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['acc.chanid','=', $param['chanid']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        if(isset($param['typedan']) && $param['typedan'] != ''){
            if($param['typedan'] == 66){
                $topid = EnginBudsubModel::where(['name'=>'项目费用','pid'=>0])->value('id');
                $sonid = EnginBudsubModel::where(['comid'=>$this->userinfo['comid'],'status'=>1])
                    ->where("id=".$topid." or topid=".$topid)->column('id');
                $where[] = ['list.budid','in', $sonid];
                $where[] = ['list.type','=', 4];
            }else{
                $where[] = ['list.type','=', 3];
                $where[] = ['list.budid','=', $param['typedan']];
            }
        }


        $data['data'] = AccountListModel::alias('list')
            ->field('list.*,acc.name as accname,acc.accnum,e.name as enginname,bid.name as bidname')
            ->leftJoin('account acc','acc.id=list.accid')
            ->leftJoin('bid bid','bid.id=list.bidid')
            ->leftJoin('engin e','e.id=list.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('account_zhi', 'list'))
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                if($item['bidid'] > 0){
                    $item['enginname'] = $item['bidname'];
                }

                if($item['type'] == 3){
                    $item['budname'] = (new PaymentService())->get_payment_budid_name($item['budid']);
                }else{
                    $item['budname'] = EnginBudsubModel::where(['id'=>$item['budid']])->value('name');
                }


                $item['accname'] = $item['accname'].'('.$item['accnum'].')';

                $item['midinfo'] = (new FinanceService())->get_account_list_midinfo($item['type'], $item['mid']);
                $item['type_name'] = (new FinanceService())->get_account_list_type_name($item['type']);



                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function show_account_shou(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = AccountListModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $account = AccountModel::get_info_by_pk($data['data']['accid']);
        $data['account'] = $account;


        if($data['data']['type'] == 1 || $data['data']['type'] == 2){
            $data['data']['budid_name'] = (new BudgetService())->get_budid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 3){
            $data['data']['budid_name'] = (new PaymentService())->get_payment_budid_name($data['data']['budid']);
        }elseif($data['data']['type'] == 4) {
            $data['data']['budid_name'] = (new BudgetService())->get_subid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 5) {
            $data['data']['budid_name'] = (new BudgetService())->get_subid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 6){
            $data['data']['budid_name'] = (new BudgetService())->get_subid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 7){
            $data['data']['budid_name'] = (new BudgetService())->get_subid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 8){
            $data['data']['budid_name'] = (new BudgetService())->get_budid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 9){
            $data['data']['budid_name'] = (new BudgetService())->get_budid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 10){
            $data['data']['budid_name'] = (new BudgetService())->get_subid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 11){
            $data['data']['budid_name'] = (new BudgetService())->get_budid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 12){
            $data['data']['budid_name'] = (new BudgetService())->get_subid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 13){
            $data['data']['budid_name'] = (new BudgetService())->get_subid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 14){
            $data['data']['budid_name'] = (new BudgetService())->get_budid_quanname($data['data']['budid']);
        }elseif($data['data']['type'] == 15) {
            $data['data']['budid_name'] = (new BudgetService())->get_subid_quanname($data['data']['budid']);
        }

        $data['midinfo'] = (new FinanceService())->get_account_list_midinfo($data['data']['type'], $data['data']['mid']);

        $data['data']['type_name'] = (new FinanceService())->get_account_list_type_name($data['data']['type']);

        $data['files'] = (new OptService())->get_file('account_list', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_account_diao(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['diao.deleted_user','=', 0];
        $where[] = ['diao.comid','=',$this->userinfo['comid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['diao.number|diao.zhai|chu.name|ru.name|diao.money|diao.shoufee|diao.baner','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['diao.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['diao.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['chuname']) && !empty($param['chuname'])){
            $where[] = ['chu.name','like', '%'.$param['chuname'].'%'];
        }
        if(isset($param['runame']) && !empty($param['runame'])){
            $where[] = ['ru.name','like', '%'.$param['runame'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['diao.money','=', $param['money']];
        }
        if(isset($param['shoufee']) && !empty($param['shoufee'])){
            $where[] = ['diao.shoufee','like', '%'.$param['shoufee'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['diao.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['diao.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['diao.status','=', $param['status']];
        }

        $data['data'] = AccountDiaoModel::alias('diao')
            ->field('diao.*,chu.name as chuname,chu.accnum as chunum,ru.name as runame,ru.accnum as runum')
            ->leftJoin('account chu','chu.id=diao.chuid')
            ->leftJoin('account ru','ru.id=diao.ruid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('account_diao', 'diao'))
            ->order('diao.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chuname'] = $item['chuname'].'('.$item['chunum'].')';
                $item['runame'] = $item['runame'].'('.$item['runum'].')';

                $item['chubudid_name'] = (new BudgetService())->get_subid_quanname($item['chubudid']);
                $item['rubudid_name'] = (new BudgetService())->get_budid_quanname($item['rubudid']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('account_diao');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_account_diao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['chuid'])){
                return outputJson(4000, '请选择调出(付款)账户');
            }
            if(empty($param['chubudid'])){
                return outputJson(4000, '请选择款项科目(支出)');
            }
            if(empty($param['ruid'])){
                return outputJson(4000, '请选择调入(收款)账户');
            }
            if(empty($param['rubudid'])){
                return outputJson(4000, '请选择款项科目(收入)');
            }

            if($param['chuid'] == $param['chubudid']){
                return outputJson(4000, '调出账户不能与调入账户相同');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入转账金额');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }

            $param['number'] = (new FinanceService())->create_account_diao_number();
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = AccountDiaoModel::insertGetId($param);
                (new FinanceService())->create_account_diao_shou($id, $param);
                (new FinanceService())->create_account_diao_zhi($id, $param);
                (new OptService())->relation_file($files,'account_diao',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'account_diao', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'account_diao', $id, '资金调拨');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new FinanceService())->create_account_diao_number();
            $data['subchuarr'] = (new BudgetService())->get_engin_budsub_guan_gong_all_xiala();
            $data['subruarr'] = (new BudgetService())->get_engin_budshou_qi_all_xiala();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_account_diao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['zhai'])){
                return outputJson(4000, '请输入款项摘要');
            }
            if(empty($param['chubudid'])){
                return outputJson(4000, '请选择款项科目(支出)');
            }
            if(empty($param['rubudid'])){
                return outputJson(4000, '请选择款项科目(收入)');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入转账金额');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                AccountDiaoModel::update($param);

                (new FinanceService())->update_account_diao_shou($param['id'], $param);
                (new FinanceService())->update_account_diao_zhi($param['id'], $param);

                (new OptService())->relation_file($files,'account_diao',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'account_diao', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'account_diao', $param['id'], '资金调拨');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AccountDiaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $chuaccount = AccountModel::get_info_by_pk($data['data']['chuid']);
            $data['chuaccount'] = $chuaccount;

            $ruaccount = AccountModel::get_info_by_pk($data['data']['ruid']);
            $data['ruaccount'] = $ruaccount;

            $chubudinfo = EnginBudsubModel::where(['id'=>$data['data']['chubudid']])->find();
            $data['chubudinfo'] = $chubudinfo;

            $rubudinfo = EnginBudshouModel::where(['id'=>$data['data']['rubudid']])->find();
            $data['rubudinfo'] = $rubudinfo;
            $data['subchuarr'] = (new BudgetService())->get_engin_budsub_guan_gong_all_xiala();
            $data['subruarr'] = (new BudgetService())->get_engin_budshou_qi_all_xiala();
            $data['files'] = (new OptService())->get_file('account_diao', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_account_diao(Request $request){
        $param = $request->post();
        $data = AccountDiaoModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            AccountDiaoModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            (new FinanceService())->del_account_diao_shou($data['id']);
            (new FinanceService())->del_account_diao_zhi($data['id']);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'account_diao', $data['id'], '资金调拨');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_account_diao(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = AccountDiaoModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $chuaccount = AccountModel::get_info_by_pk($data['data']['chuid']);
        $data['chuaccount'] = $chuaccount;

        $ruaccount = AccountModel::get_info_by_pk($data['data']['ruid']);
        $data['ruaccount'] = $ruaccount;

        $chubudinfo = EnginBudsubModel::where(['id'=>$data['data']['chubudid']])->find();
        $data['chubudinfo'] = $chubudinfo;

        $rubudinfo = EnginBudshouModel::where(['id'=>$data['data']['rubudid']])->find();
        $data['rubudinfo'] = $rubudinfo;

        $data['files'] = (new OptService())->get_file('account_diao', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_engin_chu(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['chu.deleted_user','=', 0];
        $where[] = ['chu.comid','=',$this->userinfo['comid']];


        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['shoufee']) && !empty($param['shoufee'])){
            $where[] = ['chu.shoufee','like', '%'.$param['shoufee'].'%'];
        }
        if(isset($param['zhifee']) && !empty($param['zhifee'])){
            $where[] = ['chu.zhifee','like', '%'.$param['zhifee'].'%'];
        }
        if(isset($param['koufee']) && !empty($param['koufee'])){
            $where[] = ['chu.koufee','like', '%'.$param['koufee'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['chu.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['chu.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = EnginChuModel::alias('chu')
            ->field('chu.*,e.name as enginname')
            ->join('engin e','e.id=chu.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('engin_chu', 'chu'))
            ->order('chu.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('engin_chu');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_engin_chu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if($param['shoufee'] == ''){
                return outputJson(4000, '请输入收款期初');
            }
            if($param['zhifee'] == ''){
                return outputJson(4000, '请输入付款期初');
            }
            if($param['money'] == ''){
                return outputJson(4000, '请输入余额期初');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择期初日期');
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = EnginChuModel::insertGetId($param);
                (new OptService())->relation_file($files,'engin_chu',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_chu', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_chu', $id, '项目期初');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            return outputJson(2000,lang('code_2000'), []);
        }
    }
    public function edit_engin_chu(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['shoufee'] == ''){
                return outputJson(4000, '请输入收款期初');
            }
            if($param['zhifee'] == ''){
                return outputJson(4000, '请输入付款期初');
            }
            if($param['money'] == ''){
                return outputJson(4000, '请输入余额期初');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择期初日期');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                EnginChuModel::update($param);
                (new OptService())->relation_file($files,'engin_chu',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_chu', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_chu', $param['id'], '项目期初');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginChuModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $data['files'] = (new OptService())->get_file('engin_chu', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_engin_chu(Request $request){
        $param = $request->post();
        $data = EnginChuModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            EnginChuModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'engin_chu', $data['id'], '项目期初');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_engin_chu(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = EnginChuModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
        $data['engininfo'] = $engininfo;
        $data['files'] = (new OptService())->get_file('engin_chu', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }











}
